//  -----------------------------------------------------------------------------------------------//
//   This file is part of SvnExternalUpdater                                                               //
//   Copyright (C)2008 Romain Verdier ( romain@codingly.com )                                      //
//                                                                                                 //
//   This program is free software; you can redistribute it and/or modify it under the terms       //
//   of the GNU General Public License as published by the Free Software Foundation; either        //
//   version 2 of the License, or (at your option) any later version. This program is distributed  //
//   in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied        //
//   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public  //
//   License for more details.                                                                     //
//                                                                                                 //
//   You should have received a copy of the GNU General Public License along with this program; if //
//   not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. or  //
//   see http://www.fsf.org/licensing/licenses/gpl.html                                            //
//  -----------------------------------------------------------------------------------------------//
using System.Collections.Generic;

namespace Codingly.SvnExternalUpdater.Core
{
	internal class ProjectCache
	{
		private readonly Dictionary<IRemoteFolder, Project> projectsByFolders = new Dictionary<IRemoteFolder, Project>();
		private readonly Dictionary<string, Project> projectsByPaths = new Dictionary<string, Project>();

		public IList<Project> All
		{
			get { return new List<Project>(this.projectsByFolders.Values); }
		}

		public IList<string> Paths
		{
			get { return new List<string>(this.projectsByPaths.Keys); }
		}

		public void Add(Project project)
		{
			this.projectsByFolders.Add(project.RemoteFolder, project);
			this.projectsByPaths.Add(project.Path, project);
		}

		public bool Contains(string path)
		{
			return this.projectsByPaths.ContainsKey(path);
		}

		public Project GetByRemoteFolder(IRemoteFolder remoteFolder)
		{
			Project project;
			if (this.projectsByFolders.TryGetValue(remoteFolder, out project))
			{
				return project;
			}
			return null;
		}

		public void Remove(string path)
		{
			Project project;
			if (this.projectsByPaths.TryGetValue(path, out project))
			{
				this.projectsByFolders.Remove(project.RemoteFolder);
				this.projectsByPaths.Remove(path);
			}
		}
	}
}